// pages/edit/edit.js
wx.cloud.init()
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content:'',
    noteTitle:'',
    fileList: [],
    show: false,
    classifyName: '',
    actions: [
      {
        name: '美食',
      },
      {
        name: '旅行',
      },
      {
        name: '汽车',
  
      },
    ],
    username:'',
    noteImg:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getStorage({
      key: 'username',
      success:(res)=>{
        this.setData({
          username : res.data
        })
        
      }
    })
  },
  content(e){
    this.setData({
      content: e.detail.text
    })
    // console.log(e.detail.text);
  },
  noteTitle(e){
    this.setData({
       noteTitle:e.detail
    })
    // console.log(e.detail);
  },
  afterRead(e){
    // console.log(e.detail);
    const {file} = e.detail
    wx.cloud.uploadFile({
      cloudPath:`${new Date().getTime()}.png`,
      filePath: file.url
    }).then(res=>{
      console.log(res);
      let fileList = this.data.fileList
      fileList.push({url:res.fileID})
      this.setData({
        noteImg:res.fileID,
        fileList
      })
    })

    // let obj = {}
    // obj['url'] = e.detail.file.url
    // obj['name'] = e.detail.file.thumb
    // obj['isImage'] = e.detail.file.type=="image"
    // obj['deletable'] = true
    // let fileList = this.data.fileList
    // fileList.push(obj)
    // this.setData({
    //   fileList
    // })
  },
  show(){
    this.setData({
      show: true
    })
  },
  onSelect(e){
    let classifyName = e.detail.name
    this.setData({
      classifyName
    })
    // console.log(e.detail.name);
  },
  onClose() {
    this.setData({ show: false });
  },
  publish(){
    let that = this
    request('POST','/note/publish/',{
      note_content:that.data.content,
      title:that.data.noteTitle,
      head_img:that.data.fileList[0].url,
      note_type:that.data.classifyName,
       username:that.data.username

    }).then(res=>{
      console.log(res);
    })
    
      wx.showToast({
        title: '发布成功',
      }).then(()=>{
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/noteClass/noteClass',
          })
        }, 1000);
        
      })
   
    
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})